package com.deng.maintenance.dao.impl;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.deng.common.SQLHelper;
import com.deng.maintenance.dao.IOrganizationUnitPriceItem;
import com.deng.maintenance.vo.OrganizationUnitPriceItemVo;

public class OrganizationUnitPriceItemImpl extends SQLHelper implements
		IOrganizationUnitPriceItem {

//	private final static int _PAGE_ROW = 30;
	
	public List<OrganizationUnitPriceItemVo> getPageDrugFromOrganizationUnitPriceItem(
			int page) {
		// TODO Auto-generated method stub
		return null;
	}


	public List<OrganizationUnitPriceItemVo> getPageItemFromOrganizationUnitPriceItem(
			int page) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public List<OrganizationUnitPriceItemVo> getOrganizationUnitPriceItem(int organizationId,int duty_id,
			String nameS,String typeS,int selectPoint) {
		// TODO Auto-generated method stub
		try{
			Connection conn = getConnect();
			StringBuffer sqlSB = new StringBuffer(" select UNIT_PRICE_ITEM_WEIGHT.item_name," +
					"	UNIT_PRICE_ITEM_WEIGHT.item_id," +
					"	UNIT_PRICE_ITEM_WEIGHT.price," +
//					"	case when UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION.point is not null then 1 else 0 end as is_effective," +
					"	UNIT_PRICE_ITEM_WEIGHT.item_type_name," +
					organizationId+" as organization_id," +
					"	UNIT_PRICE_ITEM_WEIGHT.item_type," +
					"	UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION.order_point," +
					"	UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION.operate_point," +
//					"	UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION.point," +
					"	UNIT_PRICE_ITEM_WEIGHT.item_name+'__'+convert(nvarchar,UNIT_PRICE_ITEM_WEIGHT.id) as sortC," +
					"	UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION.is_important," +
					duty_id+" as duty_id"+
//					"	UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION.duty_id " +
					"	from" +
					"	UNIT_PRICE_ITEM_WEIGHT" +
					"	left join  UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION" +
					"	on UNIT_PRICE_ITEM_WEIGHT.item_id = UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION.item_id" +
					"	and UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION.organization_id = " +organizationId+
					"	and UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION.duty_id = " +duty_id+
					"	where item_name like '%"+nameS+"%'");
			if(!typeS.equals("0"))
			{
				sqlSB.append("	and item_type = '"+typeS+"'");
			}
			
			if(selectPoint == 1)
			{
				sqlSB.append("  and (order_point != 0 or operate_point != 0) ");
			}
			
			sqlSB.append("   order by sortC");
			
			ResultSet rs = queryData(conn, sqlSB.toString());
			
			List<OrganizationUnitPriceItemVo> list = new ArrayList<OrganizationUnitPriceItemVo>();
			while(rs.next()){
				OrganizationUnitPriceItemVo vo = new OrganizationUnitPriceItemVo();
				vo.item_id = rs.getString("item_id");
				vo.item_name = rs.getString("item_name");
				vo.price = rs.getBigDecimal("price").toString();
//				vo.is_effective = rs.getInt("is_effective");
				vo.item_type = rs.getString("item_type");
				vo.item_type_name = rs.getString("item_type_name");
//				vo.point = rs.getBigDecimal("point");
				vo.order_point = rs.getBigDecimal("order_point")!=null?rs.getBigDecimal("order_point")+"":"";
				vo.operate_point = rs.getBigDecimal("operate_point")!=null?rs.getBigDecimal("operate_point")+"":"";;
				vo.sortC = rs.getString("sortC");
				vo.duty_id = rs.getInt("duty_id");
				vo.organization_id = organizationId;
				vo.is_important = rs.getInt("is_important");
				list.add(vo);
			}
			freeConnection(conn);
			return list;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	

	public OrganizationUnitPriceItemVo saveUpdateOrganizationVsItem(
			OrganizationUnitPriceItemVo vo) {
		try{
//			int rs = queryData("update  UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION " +
//					"	set " +
//					"	where organization_id = " +vo.organization_id+
//					"	and item_id = " +vo.item_id);
//			while(rs.next()){
//				
//				OrganizationUnitPriceItemVo tmpVo = new OrganizationUnitPriceItemVo();
//				vo.item_id = rs.getInt("item_id");
//				vo.item_name = rs.getString("item_name");
//				vo.price = rs.getBigDecimal("price").toString();
//				vo.is_effective = rs.getInt("is_effective");
//				vo.item_type = rs.getInt("item_type");
//				vo.item_type_name = rs.getString("item_type_name");
//				vo.point = rs.getInt("point");
//				vo.sortC = rs.getString("sortC");
//				vo.organization_id = rs.getInt("organization_id");
//				list.add(vo);
//			}
//			return list;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public int createOrganizationVsItem(OrganizationUnitPriceItemVo vo) {
		Connection conn = getConnect();
		int i = 0;
		try{
			
			ResultSet rs = queryData(conn, "select * from UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION where organization_id ="+vo.organization_id+
					"  and item_id = '"+vo.item_id+"' and duty_id = "+vo.duty_id);
			if(rs.next())
			{
				i = updateData(conn,"update UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION " +
						" set order_point = " +vo.order_point+",operate_point="+vo.operate_point+
						" where organization_id ="+vo.organization_id+
						" and item_id = '"+vo.item_id+"'" +
						" and duty_id = "+vo.duty_id);
			}
			else
			{
				i = updateData(conn,"insert into UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION " +
						"	(organization_id,item_id,order_point,operate_point,duty_id)" +
						"	values("+vo.organization_id+",'"+vo.item_id+"',"+vo.order_point+","
							+vo.operate_point+","+vo.duty_id+")");
			}
			freeConnection(conn);			
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return i;
	}


	public int deleteOrganizationVsItem(OrganizationUnitPriceItemVo vo) {
		Connection conn = getConnect();
		int i= 0;
		if(vo.order_point.equals(new BigDecimal(0)) && vo.operate_point.equals(new BigDecimal(0)))
		{
			i = updateData(conn,"delete UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION " +
					"	where organization_id = "+vo.organization_id+
					"	and item_id = '" +vo.item_id+"'"+
					"	and duty_id = " +vo.duty_id);
		}
		else
		{
			i = updateData(conn,"update UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION " +
					" set operate_point = " +vo.operate_point+",order_point="+vo.order_point+
					" where organization_id = "+vo.organization_id+
					" and item_id = '" +vo.item_id+"'"+
					" and duty_id = " +vo.duty_id);
		}
		
		freeConnection(conn);
		return i;
	}


	public int updateOrganizationItemPoint(OrganizationUnitPriceItemVo vo) 
	{
		int i = 0;
		try
		{
			Connection conn = getConnect();			
			if((vo.order_point.equals(new BigDecimal(0)) || vo.order_point == null) &&
				(vo.operate_point.equals(new BigDecimal(0)) || vo.operate_point == null))//删除
			{
				i = updateData(conn,"delete UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION " +
						"	where organization_id = "+vo.organization_id+
						"	and item_id = '" +vo.item_id+"'"+
						"	and duty_id = " +vo.duty_id);
			}
			else
			{				
				ResultSet rs = queryData(conn, "select * from UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION " +
						"  where organization_id ="+vo.organization_id+
						"  and item_id = '"+vo.item_id+"' " +
						"  and duty_id = "+vo.duty_id);
				if(rs.next())
				{
					i = updateData(conn,"update UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION " +
							" set order_point = " +((vo.order_point!=null&&vo.order_point!="")?vo.order_point:0)+"," +
							" operate_point="+((vo.operate_point!=null&&vo.operate_point!="")?vo.operate_point:0)+"" +
							" where organization_id ="+vo.organization_id+
							" and item_id = '"+vo.item_id+"'" +
							" and duty_id = "+vo.duty_id);
				}
				else
				{
					i = updateData(conn,"insert into UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION " +
							"	(organization_id,item_id,order_point,operate_point,duty_id)" +
							"	values("+vo.organization_id+",'"+vo.item_id+"',"+((vo.order_point!=null&&vo.order_point!="")?vo.order_point:0)+","
								+((vo.operate_point!=null&&vo.operate_point!="")?vo.operate_point:0)+","+vo.duty_id+")");
				}
			}			
			freeConnection(conn);			
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return i;
	}


	public int updateOrganizationItemImportant(OrganizationUnitPriceItemVo vo) {
		Connection conn = getConnect();
		int i = updateData(conn,"update UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION " +
				"	set is_important = " +vo.is_important+
				"	where organization_id = "+vo.organization_id+
				"	and item_id = '" +vo.item_id+"'"+
				"	and duty_id = "+vo.duty_id);
		freeConnection(conn);
		return i;
	
	}


//	public List<OrganizationUnitPriceItemVo> updateOrganizationItemPointFromHospitalConfig(int organizationId) {
//		try{
//			updateData("delete UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION " +
//					"	where organization_id = "+organizationId);
//			updateData("insert into UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION"+
//							"(organization_id,item_id,point)"+
//							"select "+organizationId+",item_id,point from UNIT_PRICE_ITEM_WEIGHT");
//			return getOrganizationUnitPriceItem(organizationId);
//		}catch(Exception ex){
//			ex.printStackTrace();
//		}
//		return null;
//	}

}
